package com.zhuang.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhuang.dto.QuestionDTO;
import com.zhuang.exception.CustomizeErrorCode;
import com.zhuang.exception.CustomizeException;
import com.zhuang.mapper.QuestionExtMapper;
import com.zhuang.mapper.QuestionMapper;
import com.zhuang.mapper.UserMapper;
import com.zhuang.pojo.Question;
import com.zhuang.pojo.QuestionExample;
import com.zhuang.pojo.User;
import com.zhuang.pojo.UserExample;
import com.zhuang.utils.CommunityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionService {

    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    QuestionExtMapper questionExtMapper;

    public PageInfo<QuestionDTO> list(int currentPage, int pageSize, String key) {
        PageHelper.startPage(currentPage, pageSize, "gmt_modified desc");
        if (key != null) {
            key = "%" + key + "%";
        }
        List<QuestionDTO> list = questionExtMapper.list(key);
        PageInfo<QuestionDTO> questionDTOPageInfo = new PageInfo<>(list);
        for (QuestionDTO questionDTO: questionDTOPageInfo.getList()) {
            UserExample example = new UserExample();
            example.createCriteria().andAccountIdEqualTo(questionDTO.getCreator());
            List<User> user = userMapper.selectByExample(example);
            questionDTO.setUser(user.get(0));
        }
        return questionDTOPageInfo;
    }

    public PageInfo<QuestionDTO> list(Integer accountId, int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize, "gmt_modified desc");
        QuestionExample example  = new QuestionExample();
        example.createCriteria().andCreatorEqualTo(accountId);
        List<Question> list = questionMapper.selectByExample(example);
        List<QuestionDTO> list1 = CommunityUtils.copyList(list, new ArrayList<>());
        PageInfo<QuestionDTO> questionDTOPageInfo = new PageInfo<>(list1);
        for (QuestionDTO questionDTO: questionDTOPageInfo.getList()) {
            UserExample example1 = new UserExample();
            example1.createCriteria().andAccountIdEqualTo(accountId);
            List<User> users = userMapper.selectByExample(example1);
            questionDTO.setUser(users.get(0));
        }
        return questionDTOPageInfo;
    }

    public QuestionDTO getQuestion(Integer id) {
        QuestionDTO byQuestionId = questionExtMapper.findByQuestionId(id);
        return byQuestionId;
    }

    public void crateOrUpdate(Question question) {
        if (question.getId() != null) {
            question.setGmtModified(System.currentTimeMillis());
            int i = questionMapper.updateByPrimaryKey(question);
            if (i != 1) {
                throw new CustomizeException(CustomizeErrorCode.QUESTION_NOT_FOUND);
            }
        } else {
            questionMapper.insert(question);
        }
    }

    public List<QuestionDTO> selectRelated(QuestionDTO questionDTO){
        String tag = questionDTO.getTag();
        if (StringUtils.isBlank(tag)) {
           return new ArrayList<>();
        }
        String[] tags = StringUtils.split(tag, ",");
        String regexpTag = Arrays.stream(tags).collect(Collectors.joining("|"));
        Question question = new Question();
        question.setTag(regexpTag);
        question.setId(questionDTO.getId());
        List<QuestionDTO> list = questionExtMapper.selectRelated(question);
        return list;
    }

    public void addViewCount(Integer id) {
        questionExtMapper.addViewCount(id);
    }
}
